<?php
/**
 * 会员管理
 * @version：1.0.0
 */

namespace App\frontend\controllers\member;

use App\frontend\controllers\UserBaseController;
use App\frontend\models\AreaModel;
use App\frontend\models\DetailsBalanceModel;
use App\frontend\models\DetailsPocketMoneyModel;
use App\frontend\models\DetailsPointsModel;
use App\frontend\models\MemberAddressModel;
use App\frontend\services\BusinessDetailLogServices;
use App\frontend\services\coupon\CouponServices;
use App\frontend\services\member\MemberServices;
use App\frontend\models\MemberModel;
use Illuminate\Database\Query\Expression;
use Illuminate\Http\Request;

class MemberController extends UserBaseController
{

    private $thisClass = 'Member';

    private static $uid = 9;

    public function __construct()
    {
        parent::__construct();
        //$this->uid = isset($this->memberInfo['uid']) ? $this->memberInfo['uid'] : $this->uid;
    }

    /**
     * 个人中心--会员首页信息
     */
    public function memberData()
    {

        $member_id = request()->input('member_id', 0);

        //$datas = self::getUserData($this->thisClass, 'memberData');

        $member_id = self::$uid;
        //获取用户信息
        $userInfo = MemberModel::getInstance()->getMember($member_id);
        //获取订单流程菜单统计数据
        $OrderProcess = MemberServices::getOrderProcessTotal($member_id);

        //返回
        $datas = [
            'nick_name' => $userInfo['nickname'],
            'avatar' => tomedia($userInfo['avatar']),
            'balance' => 100,
            'small_change' => 50,
            'integral' => 100,
            'data' => array_values($OrderProcess),
        ];

        return successJson('获取成功!', $datas);
    }

    /**
     * 会员详情
     */
    public function memberInfo()
    {
        //$list = self::getData($this->thisClass, 'memberInfo');

        //获取用户信息
        $userInfo = MemberModel::getInstance()->getMember(self::$uid);
        //获取收货地址
        $addressList = MemberAddressModel::getInstance()->getAddressList(self::$uid);
//        echo "<pre>";print_r($datas);
//        die;
        $datas = [
            'nick_name' => $userInfo['nickname'],
            'avatar' => tomedia($userInfo['avatar']),
            'gender' => $userInfo['gender'],
            'gender_name' => getGenderText($userInfo['gender']),
            'family_address' => $userInfo['address'],
            'address' => $addressList, //收货地址
        ];
        $datas = $userInfo;
        return successJson('获取成功!', $datas);
    }

    /**
     * 修改昵称
     */
    public function modifyNickName()
    {
        $name = request()->input('name');
        if (empty($name)) {
            return errorJson('不能为空');
        }

        $res = MemberModel::instance()->modifySave(['uid' => self::$uid], ['nickname' => $name]);
        if ($res) {
            return successJson('更新成功!');
        } else {
            return errorJson('更新失败!');
        }

    }


    /**
     * 修改登录密码
     */
    public function editLoginPass()
    {
        //$list = self::getData($this->thisClass, 'editLoginPass');

        $old_pass = request()->input('old_pass');
        $new_pass = request()->input('new_pass');
        $confirm_pass = request()->input('confirm_pass');
        $res = MemberServices::editPassword($old_pass, $new_pass, $confirm_pass, self::$uid);
        if ($res['status'] == 0) {
            return successJson($res['msg']);
        } else {
            return errorJson($res['msg']);
        }
    }

    /**
     * 设置支付密码
     */
    public function setPayPass()
    {
        $list = self::getData($this->thisClass, 'setPayPass');

        $pay_pass = request()->input('pass');
        $res = MemberServices::setPayPassword($pay_pass, self::$uid);
        if ($res['status'] == 0) {
            return successJson($res['msg']);
        } else {
            return errorJson($res['msg']);
        }

    }

    /**
     * 修改支付密码
     */
    public function editPayPass()
    {
        // $list = self::getData($this->thisClass, 'editPayPass');

        $old_pass = request()->input('old_pay_pass');
        $new_pass = request()->input('new_pay_pass');
        $confirm_pass = request()->input('confirm_pass');
        $res = MemberServices::editPayPassword($old_pass, $new_pass, $confirm_pass, self::$uid);
        if ($res['status'] == 0) {
            return successJson($res['msg']);
        } else {
            return errorJson($res['msg']);
        }
    }

    /**
     * 获取钱包余额
     */
    public function getWalletBalance()
    {
        $list = self::getData($this->thisClass, 'getWalletBalance', 2);

        //获取用户信息
        $filed = ['balance', 'pocket_money'];
        $userInfo = MemberServices::getUserInfoById(self::$uid, $filed);
        return successJson('获取成功!', $userInfo);
    }

    /**
     * 余额明细
     */
    public function getBalanceDetails()
    {
        //$list = self::getData($this->thisClass, 'getBalanceDetails', 2);

        $page = request()->input('page', 1);
        $pageSize = request()->input('pageSize', 5);
        //获取明细
        $condition = [];
        $condition['uid'] = self::$uid;
        $list = DetailsBalanceModel::getInstance()->getList($condition, $page, $pageSize);
        return  successJson('获取成功!', $list);
    }

    /**
     * 充值
     */
    public function recharge()
    {
        $list = self::getData($this->thisClass, 'recharge',2);





        /**
         * 此处只有保存日志逻辑--20230509
         */
        $money = 20;
        $info = ['uid' => self::$uid];
        //增加余额明细
        $res = BusinessDetailLogServices::SaveDetailsLog(2, 'recharge', $money, $info);
        if ($res['status'] != 0) {
            return errorJson($res['msg']);
        }
        //增加余额
        $condition = ['uid' => self::$uid];
        MemberModel::getInstance()->modifySave($condition, ['balance'=>new Expression('balance + '.$money)]);
        return successJson('充值成功!', $list);
    }

    /**
     * 零钱明细
     */
    public function getCommissionDetails()
    {
        $list = self::getData($this->thisClass, 'getCommissionDetails',2);

        $page = request()->input('page', 1);
        $pageSize = request()->input('pageSize', 5);
        //获取明细
        $condition = [];
        $condition['uid'] = self::$uid;
        $list = DetailsPocketMoneyModel::getInstance()->getList($condition, $page, $pageSize);
        return successJson('获取成功!', $list);
    }

    /**
     * 提现佣金
     */
    public function withdrawalCommission()
    {
        $list = self::getData($this->thisClass, 'withdrawalCommission');

        return successJson('提现成功!', $list);
    }

    /**
     * 积分明细
     */
    public function getPointsDetails()
    {
        //$list = self::getData($this->thisClass, 'getPointsDetails',2);

        $page = request()->input('page', 1);
        $pageSize = request()->input('pageSize', 5);
        //获取明细
        $condition = [];
        $condition['uid'] = self::$uid;
        $list = DetailsPointsModel::getInstance()->getList($condition, $page, $pageSize);
        return successJson('获取成功!', $list);
    }

    /**
     * 我的服务列表
     */
    public function getMyServiceList()
    {
        $list = self::getData($this->thisClass, 'getMyServiceList');

        return successJson('获取成功!', $list);
    }

    /**
     * 我的服务详情
     */
    public function getMyServiceDetails()
    {
        $list = self::getData($this->thisClass, 'getMyServiceDetails');

        return successJson('获取成功!', $list);
    }

    /**
     * 核销订单
     */
    public function verifOrders()
    {
        $list = self::getData($this->thisClass, 'verifOrders');

        return successJson('获取成功!', $list);
    }

    /**
     * 我的卡券
     */
    public function getMyCardCoupon(Request $request)
    {
        //$list = self::getData($this->thisClass, 'getMyCardCoupon');
        $data = $request->all();
        $data['member_id'] = 1;//self::$uid;
        $list = CouponServices::getUserCouponList($data);

        return successJson('获取成功!', $list);
    }

}
